package com.jike.noobmoney.mvp.view.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.QuestionListAdapter;
import com.jike.noobmoney.entity.QuestionEntity;
import com.jike.noobmoney.net.RetrofitHelper;
import com.jike.noobmoney.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TestActivity extends BaseActivity {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    List<QuestionEntity.QuestionBean> mData = new ArrayList<>();
    private QuestionListAdapter adapter;

    @Override
    protected int layoutResID() {
        return R.layout.activity_test;
    }

    @Override
    protected void initData() {

        loadData();

        //adapter
        adapter = new QuestionListAdapter(context);
        //manager
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(refreshlayout -> {
            loadData();
        });


    }

    /**
     * 获取数据
     */
    private void loadData() {
        Call<QuestionEntity> question = RetrofitHelper.getInstance().getQuestion();
        question.enqueue(new Callback<QuestionEntity>() {
            @Override
            public void onResponse(Call<QuestionEntity> call, Response<QuestionEntity> response) {
                if (response != null) {
                    QuestionEntity body = response.body();
                    if (body != null) {
                        if ("100".equals(body.getCode())) {
                            List<QuestionEntity.QuestionBean> questionList = body.getQuestion();
                            if (questionList != null && questionList.size() > 0) {
                                mData.clear();
                                mData.addAll(questionList);
                            }
                            adapter.refresh(mData);
                        } else {
                            ToastUtils.showShortToastSafe(body.getRinfo());
                        }
                    }
                }
                refreshLayout.finishRefresh();
            }

            @Override
            public void onFailure(Call<QuestionEntity> call, Throwable t) {
                ToastUtils.showShortToastSafe(t.getMessage());
                refreshLayout.finishRefresh();
            }
        });

    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

}
